package com.atguigu.structures.tree.node;

/**
 * 哈夫曼树节点-带数据和权值
 *
 * @author xr-8
 * @date 2022-12-04
 * @time 23:14
 */
public class HuffmanNode implements Comparable<HuffmanNode> {
    public Byte data; // 存放数据(字符)本身，比如'a' => 97 ' ' => 32
    public int weight; // 结点权值, 表示字符出现的次数
    public HuffmanNode left; // 指向左子结点
    public HuffmanNode right; // 指向右子结点

    public HuffmanNode(int weight) {
        this.weight = weight;
    }

    public HuffmanNode(Byte data, int weight) {
        this.data = data;
        this.weight = weight;
    }

    //前序遍历
    public void preOrder() {
        System.out.println(this);
        if(this.left != null) {
            this.left.preOrder();
        }
        if(this.right != null) {
            this.right.preOrder();
        }
    }

    @Override
    public String toString() {
        return "Node [data = " + data + " weight=" + weight + "]";
    }

    @Override
    public int compareTo(HuffmanNode o) {
        // 表示从小到大排序
        return this.weight - o.weight;
    }
}
